package com.gnerv.battle.boot.project.guard.business.impl;

import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import com.gnerv.battle.boot.project.guard.business.MonitorDatasourceBusiness;
import com.gnerv.battle.boot.project.guard.model.bo.MonitorDatasourceBO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorDatasourceCreateDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorDatasourceDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorDatasourceUpdateDTO;
import com.gnerv.battle.boot.project.guard.service.MonitorDatasourceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorDatasourceBusinessImpl implements MonitorDatasourceBusiness {

    private final MonitorDatasourceService monitorDatasourceService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(MonitorDatasourceCreateDTO monitorDatasourceCreateDTO) {
        MonitorDatasourceBO monitorDatasourceBO = monitorDatasourceCreateDTO.toMonitorDatasourceBO();
        monitorDatasourceBO.setUkId(UUIDTools.lower());
        monitorDatasourceService.create(monitorDatasourceBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        monitorDatasourceService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(MonitorDatasourceUpdateDTO monitorDatasourceUpdateDTO) {
        monitorDatasourceService.updateByUkId(monitorDatasourceUpdateDTO.toMonitorDatasourceBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<MonitorDatasourceDTO> selectByUkId(String ukId) {
        return RestResult.ok(monitorDatasourceService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<MonitorDatasourceDTO>> listByName(String name, Integer page, Integer size) {
        return RestResult.ok(monitorDatasourceService.listByName(name, page, size));
    }

}
